#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/14  22:42
# @Author: 余浪人
# @email: yulangren520@gmail.com

from apps.models import db, BaseModel
from apps.models.column_model import Column_model


class Collector_Model(BaseModel):
    project_name = db.Column(db.String(64), comment='项目名称')
    class_lD = db.Column(db.Integer, db.ForeignKey(Column_model.id), comment='所属栏目')
    rule_cls = db.Column(db.Integer, comment='规则格式')
    agreement_cls = db.Column(db.Integer, comment='协议格式')
    fixed_URL = db.Column(db.String(512), comment='指定URL')
    goal_url = db.Column(db.String(128), comment='拼接URL')
    URL_create = db.Column(db.String(512), comment='URL批量生成')
    create_range = db.Column(db.String(20), comment='生成范围_格式:1|99')
    create_is_range = db.Column(db.Integer, comment='是否批量生成|1_是|0_否')
    img_save_path = db.Column(db.String(512), comment='图片文件本地保存目录')
    img_is_save = db.Column(db.Integer, comment='是否保存图片|1_是|0_否')
    url_is_desc = db.Column(db.Integer, comment='是否倒序采集|1_是|0_否')
    tim_acquisition = db.Column(db.Integer, comment='是否定时采集|1_是|0_否')
    url_relative = db.Column(db.Integer, comment='相对地址|1_是|0_否')
    img_relative = db.Column(db.Integer, comment='图片相对地址|1_是|0_否')
    replace_content = db.Column(db.Text, comment='内容替换')
    list_rule = db.Column(db.String(1024), comment='列表规则')
    URL_rule = db.Column(db.String(1024), comment='链接规则')
    img_url_rule = db.Column(db.String(1024), comment='列表页小图URL规则')
    title_rule = db.Column(db.String(1024), comment='标题规则')
    content_rule = db.Column(db.String(1024), comment='正文规则')
    time_rule = db.Column(db.String(1024), comment='时间规则')
    keys_rule = db.Column(db.String(1024), comment='关键词规则')
    author_rule = db.Column(db.String(1024), comment='作者规则')
    source_rule = db.Column(db.String(1024), comment='来源规则')
    clicks_rule = db.Column(db.String(1024), comment='点击规则')
    description_rule = db.Column(db.String(1024), comment='描述规则')
    restrictive = db.Column(db.Integer, default=0, comment='采集限制|默认不限制')
    set_time = db.Column(db.Integer, default=0, comment='自动采集时间 单位:秒')
    is_start = db.Column(db.Integer, default=0, comment='是否开始采集|1开始|0未开始')
    filterFrame = db.Column(db.Integer, comment='过滤iframe|1_是|0_否')
    filterObject = db.Column(db.Integer, comment='过滤Object|1_是|0_否')
    filterScript = db.Column(db.Integer, comment='过滤Script|1_是|0_否')
    filterDiv = db.Column(db.Integer, comment='过滤Div|1_是|0_否')
    filterStyle = db.Column(db.Integer, comment='过滤Style|1_是|0_否')
    filterTable = db.Column(db.Integer, comment='过滤Table|1_是|0_否')
    filterSpan = db.Column(db.Integer, comment='过滤Span|1_是|0_否')
    filterImg = db.Column(db.Integer, comment='过滤Img|1_是|0_否')
    filterFont = db.Column(db.Integer, comment='过滤Font|1_是|0_否')
    filterA = db.Column(db.Integer, comment='过滤A |1_是|0_否')
    filterP = db.Column(db.Integer, comment='过滤P |1_是|0_否')
    filterAll = db.Column(db.Integer, comment='过滤全部标签 |1_是|0_否')
    pro = db.relationship('Column_model', backref='collector_model')

    def __repr__(self):
        return f'采集规则:<{self.project_name}>'

    def keys(self):
        return 'project_name', 'class_lD', 'rule_cls','agreement_cls', 'encode_code', 'URL', 'img_save_path', 'img_is_save', 'url_is_desc', 'tim_acquisition', 'replace_content', 'list_rule', 'URL_rule', 'img_url_rule', 'title_rule', 'content_rule', 'keys_rule', 'time_rule', 'author_rule', 'source_rule', 'restrictive', 'URL_create', 'create_range', 'create_is_range', 'set_time', 'is_start', 'img_url_rule', 'goal_url', 'url_relative','img_relative', 'filterFrame', 'filterObject', 'filterScript', 'filterDiv', 'filterStyle', 'filterTable', 'filterSpan', 'filterImg', 'filterFont', 'filterA', 'filterP', 'filterAll', 'description_rule', 'clicks_rule', 'fixed_URL'


# 采集历史记录
class Collect_History_Model(BaseModel):
    title = db.Column(db.String(255), comment='内容标题')
    identifying_code = db.Column(db.String(128), index=True, comment='标识码|指纹')
    superior = db.Column(db.Integer, db.ForeignKey(Column_model.id), comment='所属栏目')
    pro = db.relationship('Column_model', backref='collect_history_model')

    def __repr__(self):
        return f'采集记录:<{self.title}>'

    def keys(self):
        return 'title', 'identifying_code', 'superior',


# 采集代理
class Collect_Proxy_Model(BaseModel):
    is_proxy = db.Column(db.Integer, default=0, comment='是否代理')
    app_key = db.Column(db.String(32), default='', comment='app_key')

    def keys(self):
        return 'is_proxy', 'app_key'
